package com.qing.library.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * Created by DaiQing.Zha on 2016/6/8 0008.
 * Email:1642674371@qq.com
 * Description:
 */
public class StringUtil {

    /**
     * 判断给定字符串是否空白串,空白串是指由空格、制表符、回车符、换行符组成的字符串
     * @param input
     * @return boolean 若字符串为空，返回true；若字符串不为空，返回false。
     */
    public static boolean isEmpty(String input) {

        if (input == null || "".equals(input)) {
            return true;
        }

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
                return false;
            }
        }
        return true;
    }

    /**
     * 将16进制编码变成字符串模式；例如byte[]{0xfe 0x12 0x03}变成 FE1203
     */
    public static String hex2String(byte[] bytes) {
        String str = "";
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(bytes[i] & 0xFF).toUpperCase();
            if (hex.length() == 1) {
                hex = "0" + hex;
            }
            str = str + hex;
        }
        return str;
    }

    /**
     * 将字符串变成16进制编码模式，例如FE1203变成byte[]{0xfe 0x12 0x03}
     */
    public static byte[] string2Hex(String str) {
        if (str.length() % 2 == 1) {
            str = "0" + str;
        }
        int strLen_2 = str.length() / 2;
        byte[] bytes = new byte[strLen_2];
        for (int i = 0; i < strLen_2; i++) {
            String s = str.substring(i * 2, i * 2 + 2);
            byte b = (byte) Integer.parseInt(s, 16);
            bytes[i] = b;
        }
        return bytes;
    }

    /**
     * 二进制数组转化为16进制字符串
     * @param src
     * @return
     */
    public static String bytesToHexString(byte[] src){
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }
    /**
     * Convert hex string to byte[]
     * @param hexString the hex string
     * @return byte[]
     */
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    public static String asciiStringToHex(String str) {
        char[] chars = str.toCharArray();
        StringBuffer hex = new StringBuffer();
        for (int i = 0; i < chars.length; i++) {
            hex.append(Integer.toHexString((int) chars[i]));
        }
        return hex.toString();
    }
    /**
     * Convert char to byte
     * @param c char
     * @return byte
     */
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    public static byte[] characterTobyte(String character){

        return character.getBytes();
    }

    /**
     * long转byte
     * @param number
     * @return
     */
    public static byte[] converLongToBytes(long number) {

        long temp = number;
        byte[] b = new byte[8];
        for (int i = 0; i < b.length; i++) {
            b[i] = new Long(temp & 0xff).byteValue();// 将最低位保存在最低位
            temp = temp >> 8; // 向右移8位
        }
        return b;
    }

    /**
     * byte转long
     * @param byt
     * @return
     */
    public static long byteToLong(byte[] byt) {
        long l = 0;
        ByteArrayInputStream bais = new ByteArrayInputStream(byt);
        DataInputStream dis = new DataInputStream(bais);
        try {
            l = dis.readLong();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return l;
    }


    /**
     * 16进制字符串转ascii
     * @param s
     * @return
     */
    public static String string2Ascii(String s) {
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(
                        i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            s = new String(baKeyword, "ASCII");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }

    /**
     * 字符串转16进制字符串
     * @param str
     * @return
     */
    public static String str2HexStr(String str) {
        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = str.getBytes();
        int bit;
        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f;
            sb.append(chars[bit]);
        }
        return sb.toString();
    }
}
